package com.gjintel.framework.holidayset.dao.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.gjintel.framework.holidayset.dao.IHolidaySetDao;
import com.gjintel.framework.holidayset.model.HolidaySet;
import com.gjintel.sdk.base.dao.HibernateGenericDAO;
import com.gjintel.sdk.base.dao.Page;
import com.gjintel.sdk.base.dao.Sort;
import com.gjintel.sdk.util.StringUtil;

/**
 * @author
 * @description 节假日数据访问实现
 */
@Repository("holidaySetDao")
public class HolidaySetDaoImpl extends HibernateGenericDAO implements IHolidaySetDao {

    @Override
    public void add(HolidaySet holidaySet) {
        this.save(holidaySet);
    }

    @Override
    public void updateByDate(String date, String userName, String descript) {
        StringBuilder hql = new StringBuilder("UPDATE HolidaySet s SET s.hoilday_type = 2,s.create_name = '");
        hql.append(userName);
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date2 = new Date();
        String temp = sdf1.format(date2);
        hql.append("' ,s.updateTime = '").append(temp).append("'");
        hql.append(" ,s.descript = '").append(descript).append("'");
        hql.append(" WHERE s.at_day = '").append(date).append("'");
        this.executeUpdate(hql.toString());
    }


    @SuppressWarnings("unchecked")
    @Override
    public List<String> findAllByYear(String thisYear, String nextYear) {
        String hql = "select s.at_day from HolidaySet s where delete_flag = 0 and hoilday_type != 2 and (s.at_year>= '" + thisYear + "' and s.at_year <= '" + nextYear + "')";
        return this.find(hql);
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<String> findByYear(String thisYear, String nextYear) {
        String hql = "select s.at_day from HolidaySet s where delete_flag = 0 and (s.at_year= '" + thisYear + "' or s.at_year = '" + nextYear + "')";
        return this.find(hql);
    }

    @Override
    public void findAll(Page page, HolidaySet holidaySet, Sort sort) {
        StringBuilder hql = new StringBuilder("from HolidaySet s where 1=1 and s.delete_flag = 0 ");
        List<String> params = new ArrayList<String>(1);
        if (holidaySet != null) {
            if (!StringUtil.isEmpty(holidaySet.getAt_day())) {
                hql.append(" and s.at_day like ? ");
                params.add("%" + holidaySet.getAt_day().trim() + "%");
            }
            if (!StringUtil.isEmpty(holidaySet.getAt_year())) {
                hql.append(" and s.at_year like ? ");
                params.add("%" + holidaySet.getAt_year().trim() + "%");
            }
        }
        // hql.append(" order by s.updateTime desc");

        //设置排序
        sort.setFields("at_day");
        sort.setOrders("desc");
        this.findAsPage(hql.toString(), page, sort, "s", true, params.toArray());
    }

    @Override
    public HolidaySet getHolidaySetbyDate(String date) {
        return (HolidaySet) this.findUnique("from HolidaySet s where s.delete_flag = 0 and s.at_day=?", date);
    }

    //删除指定日期
    @Override
    public int deleteById(String Id) {
        String hql = "delete from HolidaySet s where 1=1 and s.id = '" + Id + "'";
        return this.executeUpdate(hql);
    }
}
